#!/bin/sh

# Prepare customer-specific x500 org name and replace default value in provided config files
x500_org_name=$(awk 'BEGIN { srand(); print int(rand()*32768) }' /dev/null | md5sum | head -c 10)
sed -i "s/<x500_org_name>/$x500_org_name/" /etc/gromox/midb.cfg
sed -i "s/<x500_org_name>/$x500_org_name/" /etc/gromox/zcore.cfg
sed -i "s/<x500_org_name>/$x500_org_name/" /etc/gromox/autodiscover.ini

# Create additional configuration files containing only x500_org_name
for item in exchange_emsmdb exchange_nsp exmdb_local exmdb_provider
do
	if [ -f "/etc/gromox/$item.cfg" ]
	then
		echo "$item.cfg already exists."
	else
		echo "Copying new $item.cfg."
		install -m 0640 -o grommunio -g gromoxcf /dev/null /etc/gromox/$item.cfg && \
			grep x500_org_name >> /etc/gromox/$item.cfg < /etc/gromox/zcore.cfg || echo "ERROR: Copying new $item.cfg failed."
	fi
done

exit 0
